package top.atcra.rpc.demo.idGeneratorTest;

import top.atcra.rpc.RPCBootstrap;
import top.atcra.rpc.transport.utils.RequestIdGenerator;

import java.util.concurrent.atomic.AtomicLong;

public class IdGeneratorMockUtil {
    static class MockIdGenerator extends RequestIdGenerator {

        public MockIdGenerator(long dataCenterId, long machineId) {
            super(dataCenterId, machineId);
        }
        long id1 = 0;
        AtomicLong id2 = new AtomicLong(0);
        @Override
        public synchronized long getId2(){
            return ++id1;
//            return id2.incrementAndGet();
        }
    }

    static void replaceIDGenerator(RPCBootstrap client){
        try {
            var generator = client.getClass().getDeclaredField("requestIdGenerator");
            generator.setAccessible(true);
            generator.set(client, new MockIdGenerator(0,0));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
